/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.PixelFormat
import android.text.Editable
import android.text.TextWatcher
import android.text.method.PasswordTransformationMethod
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.ecarx.systemui.plugin.AppConstants
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.utils.LogUtils


class InputTextDialog : Dialog, View.OnClickListener {

    companion object {
        private const val TAG = "InputTextDialog"
    }

    private var mEditText:EditText? = null
    private var mIvVisible:ImageView? = null
    private var mInputTextListener:InputTextListener? = null
    private var isPwdMode = true
    constructor(context: Context?, isPwd: Boolean, inputTextListener: InputTextListener) : super(context!!, R.style.PluginDialog) {
        mInputTextListener = inputTextListener
        isPwdMode = isPwd
        init()
    }
    constructor(context: Context?, themeResId: Int) : super(
        context!!, themeResId
    ) {
        init()
    }

    private fun init(){
        initConfig()
        setContentView(R.layout.dialog_input_text)
        initView()
    }

    private fun initView() {
        val btnConfirm = findViewById<Button>(R.id.btn_confirm)
        btnConfirm.setOnClickListener(this)
        findViewById<Button>(R.id.btn_cancel).setOnClickListener(this)

        mIvVisible = findViewById<ImageView>(R.id.iv_visible)
        if (isPwdMode) {
            mIvVisible?.visibility = View.VISIBLE
            mIvVisible?.setOnClickListener(this)
            findViewById<TextView>(R.id.tv_desc).visibility = View.VISIBLE
            btnConfirm.isEnabled = false
            mIvVisible?.isSelected = true
            updateEdittext(true)
        } else {
            mIvVisible?.visibility = View.GONE
            findViewById<TextView>(R.id.tv_desc).visibility = View.GONE
            btnConfirm.isEnabled = true
            mIvVisible?.isSelected = false
            updateEdittext(false)
        }


        findViewById<ImageView>(R.id.iv_clear).setOnClickListener(this)

        mEditText = findViewById<EditText>(R.id.edit_text)
        mEditText?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                if (isPwdMode) {
                    btnConfirm.isEnabled = s.toString().length > 8
                    LogUtils.d(TAG, "afterTextChanged: btnConfirm.isEnabled = ${btnConfirm.isEnabled }")
                }
            }
        })
    }

    private fun initConfig() {
        val window = window
        if (window != null) {
            val params = WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                AppConstants.PLUGIN_DIALOG_TYPE,
                WindowManager.LayoutParams.FLAG_FULLSCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT
            )
            params.gravity = Gravity.CENTER
            window.attributes = params
            setCanceledOnTouchOutside(false)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_cancel -> {
                dismiss()
            }
            R.id.btn_confirm -> {
                val content = mEditText?.text.toString()
                mInputTextListener?.onInput(content)
                dismiss()
            }
            R.id.iv_visible -> {
                mIvVisible!!.isSelected = !mIvVisible!!.isSelected
                updateEdittext(mIvVisible!!.isSelected)
            }
            R.id.iv_clear -> {
                mEditText?.setText("")
            }
        }
    }

    private fun updateEdittext(isSelected: Boolean){
        if (isSelected) {
            mEditText?.transformationMethod = PasswordTransformationMethod()
        } else {
            mEditText?.transformationMethod = null
        }
    }

    override fun show() {
        super.show()
        mEditText?.requestFocus()
    }

    interface InputTextListener {
        fun onInput(text: String)
    }
}